package xin.marcher.module.quality.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.quality.domain.dto.ItemQualityControlDataDTO;
import xin.marcher.module.quality.domain.dto.ItemQualityControlDataResultDTO;
import xin.marcher.module.quality.domain.request.ItemQualityControlDataRequest;
import xin.marcher.module.quality.service.ItemQualityControlDataService;

import java.util.List;

/**
 *
 */
@Slf4j
@RestController
@RequestMapping("/quality/riskData")
public class ItemQualityControlDataController {

    @Autowired
    private ItemQualityControlDataService itemQualityControlDataService;

    /**
     * 获取风险商品列表
     *
     * @param request
     * @return
     */
    @PreAuthorize("@rights.check('riskSkuList', #token)")
    @PostMapping("/riskSkuList")
    public JsonResult<List<ItemQualityControlDataDTO>> riskSkuList(String token, @RequestBody ItemQualityControlDataRequest request) {
        try {
            return JsonResult.buildSuccess(itemQualityControlDataService.riskSkuList(request));
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 关闭风险数据，流入白名单
     *
     * @param request
     * @return
     */
    @PreAuthorize("@rights.check('closedRiskData', #token)")
    @PostMapping("/closedRiskData")
    public JsonResult<ItemQualityControlDataResultDTO> closedRiskData(String token, @RequestBody ItemQualityControlDataRequest request) {
        try {
            return JsonResult.buildSuccess(itemQualityControlDataService.closedRiskData(request));
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

}
